﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.settings
{
    public partial class quanzi_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString();//操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.quanzi().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.View.ToString()); //检查权限
                GetProvince();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定省份=================================
        private void GetProvince()
        {
            BLL.province bll = new BLL.province();
            DataTable dt = bll.GetList(0, "", "id asc").Tables[0];

            this.ddlProvince.Items.Clear();
            this.ddlProvince.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["province_name"].ToString().Trim();

                this.ddlProvince.Items.Add(new ListItem(Title, Id));
            }
            
        }
        #endregion

        #region 绑定城市=================================
        private void GetCity(int province_id)
        {
            BLL.city bll = new BLL.city();
            DataTable dt = bll.GetList(0, "province_id=" + province_id, "id asc").Tables[0];

            this.ddlCity.Items.Clear();
            this.ddlCity.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["city_name"].ToString().Trim();

                this.ddlCity.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetDistrict(int city_id)
        {
            BLL.district bll = new BLL.district();
            DataTable dt = bll.GetList(0, "city_id=" + city_id, "id asc").Tables[0];

            this.ddlDistrict.Items.Clear();
            this.ddlDistrict.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["district_name"].ToString().Trim();

                this.ddlDistrict.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.quanzi bll = new BLL.quanzi();
            Model.quanzi model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();
            txtTitle.Text = model.title;
            txtShortTitle.Text = model.short_title;
            rblCategoryID.SelectedValue = model.category_id.ToString();

            ddlProvince.SelectedValue = model.province_id.ToString();
            if (model.province_id > 0)
            {
                GetCity(model.province_id);
            }
            ddlCity.SelectedValue = model.city_id.ToString();
            if (model.city_id > 0)
            {
                GetDistrict(model.city_id);
            }
            ddlDistrict.SelectedValue = model.district_id.ToString();

            txtZuobiao.Text = model.zuobiao;
            txtSortId.Text = model.sort_id.ToString();
            txtRemark.Text = model.remark.ToString();

            txtSeoKeywords.Text = model.seo_keywords;
            txtSeoDescription.Text = model.seo_description;

        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.quanzi model = new Model.quanzi();
            BLL.quanzi bll = new BLL.quanzi();

            model.title = txtTitle.Text.Trim();
            model.short_title = txtShortTitle.Text.Trim();
            model.category_id = Utils.StrToInt(rblCategoryID.SelectedValue,0);
            model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
            model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
            model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);
            model.zuobiao = txtZuobiao.Text.Trim();
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
            model.zuobiao = txtZuobiao.Text.Trim();
            model.remark = txtRemark.Text;
            model.seo_keywords = txtSeoKeywords.Text;
            model.seo_description = txtSeoDescription.Text;

            if (bll.Add(model) > 0)
            {
                return true;
            }

            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.quanzi bll = new BLL.quanzi();
            Model.quanzi model = bll.GetModel(_id);

            model.title = txtTitle.Text.Trim();
            model.short_title = txtShortTitle.Text.Trim();
            model.category_id = Utils.StrToInt(rblCategoryID.SelectedValue, 0);
            model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
            model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
            model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);
            model.zuobiao = txtZuobiao.Text.Trim();
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
            model.zuobiao = txtZuobiao.Text.Trim();
            model.remark = txtRemark.Text;

            model.seo_keywords = txtSeoKeywords.Text;
            model.seo_description = txtSeoDescription.Text;

            if (bll.Update(model))
            {
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改信息成功！", "quanzi_list.aspx", "parent.loadMenuTree");
            }
            else //添加
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加信息成功！", "quanzi_list.aspx", "parent.loadMenuTree");
            }
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            int province_id = int.Parse(ddlProvince.SelectedValue);
            GetCity(province_id);
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            int city_id = int.Parse(ddlCity.SelectedValue);
            GetDistrict(city_id);
        }
    }
}